clear all 
set more off

// Read in the data
use Data_besparty.dta, clear

// Create respondent-party variable
tostring id, gen(id_str)
gen id_party = id_str + "_" + party

*****************************
** Robustness
*****************************

// Table OA3.1
reg perc_ch ChangeInEuPosit copartisan age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id)
est store accuracy_lc_r1
reg perc_ch ChangeInImmigEuPosit copartisanFirst age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id)
est store accuracy_lc_r2
reg perc_ch ChangeInImmigEuPosit copartisan age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id_party)
est store accuracy_lc_r3

// Table OA3.2
reg perc_ch ChangeInEuPosit copartisan age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id) 
est store accuracy_ul_r1
reg perc_ch ChangeInImmigEuPosit copartisanFirst age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id)
est store accuracy_ul_r2
reg perc_ch ChangeInImmigEuPosit copartisan age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id_party) 
est store accuracy_ul_r3

// Table OA4.1
ologit partyPositionCertainty absChangeInImmigEuPosit prevPartyPositionCertainty copartisan age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id)
est store certainty_lc_r1
ologit partyPositionCertainty absChangeInEuPosit copartisan age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id)
est store certainty_lc_r2
ologit partyPositionCertainty absChangeInImmigEuPosit copartisanFirst age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id)
est store certainty_lc_r3
ologit certaintyWithDK2 absChangeInImmigEuPosit copartisan age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id)
est store certainty_lc_r4
ologit partyPositionCertainty absChangeInImmigEuPosit copartisan age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id_party)
est store certainty_lc_r5

// Table OA4.2
ologit partyPositionCertainty absChangeInImmigEuPosit prevPartyPositionCertainty copartisan age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id)
est store certainty_ul_r1
ologit partyPositionCertainty absChangeInEuPosit copartisan age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id)
est store certainty_ul_r2
ologit partyPositionCertainty absChangeInImmigEuPosit copartisanFirst age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id)
est store certainty_ul_r3
ologit certaintyWithDK2 absChangeInImmigEuPosit copartisan age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id)
est store certainty_ul_r4
ologit partyPositionCertainty absChangeInImmigEuPosit copartisan age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id_party)
est store certainty_ul_r5

*****************************
** Tables OA6.1-6.2
*****************************

// Create squared variables
gen ChangeInImmigEuPosit2 = ChangeInImmigEuPosit^2
gen absChangeInImmigEuPosit2 = absChangeInImmigEuPosit^2

ologit partyPositionCertainty absChangeInImmigEuPosit absChangeInImmigEuPosit2 copartisan age male education_numeric i.wave if party!="LD" & party!="UKIP", vce(cluster id) 
est store certainty_quad_divided
ologit partyPositionCertainty absChangeInImmigEuPosit absChangeInImmigEuPosit2 copartisan age male education_numeric i.wave if party!="Con" & party!="Lab", vce(cluster id)
est store certainty_quad_united

*****************************
** Table OA7.1
*****************************

// Calculate average updating value for each party in each wave
bysort party wave : egen mean_updating = mean(perc_ch) 
// Calculcate average certainty value for each party in each wave.
bysort party wave : egen mean_certainty = mean(partyPositionCertainty)
// Keep only party-level observations
keep party wave mean_updating mean_certainty ChangeInImmigEuPosit absChangeInImmigEuPosit
duplicates drop
drop if ChangeInImmigEuPosit==.

pwcorr mean_updating ChangeInImmigEuPosit if (party=="Con"|party=="Lab"), sig star(.05)
pwcorr mean_certainty absChangeInImmigEuPosit if (party=="Con"|party=="Lab"), sig star(.05) 
pwcorr mean_updating ChangeInImmigEuPosit if (party=="UKIP"|party=="LD"), sig star(.05)
pwcorr mean_certainty absChangeInImmigEuPosit if (party=="UKIP"|party=="LD"), sig star(.05)

***************************
** OA2: Correlation between proprotion of issue mentions by party-wave in the CCD and salience of the issue by time
***************************

// Read in CCD
use Data_party_subset.dta, replace

// sum up all issue proportions
egen rowtotal = rowtotal(share_Taxes_positive-share_Other_Issue_neutral)

// sum up all European integration proportions
egen eutotal = rowtotal(share_Immigration_positive share_Immigration_negative share_Immigration_neutral share_EU_positive share_EU_negative share_EU_neutral)

// Could be more salient in w2 and w7. Create binary variable
gen salience_binary = 1 if bes_wave==2 | bes_wave==7
replace salience_binary = 0 if bes_wave!=2 & bes_wave!=7

// Correlation between position and salience (time)
pwcorr eutotal salience_binary, sig star(.05)

***************************
** OA2: Correlation between proprotion of issue mentions by party-wave in the CCD and divisiveness
***************************

// Create divisiveness variable
generate divided = .
replace divided = 1 if partyname=="Con"|partyname=="Lab"
replace divided = 0 if partyname=="LD"|partyname=="UKIP"

// Correlation between party discussions and divisiveness
pwcorr eutotal divided, sig star(.05)

// Save the data to use later
sort partyname bes_wave
save "Data_ccdparty", replace

***************************
** OA2: Correlation between proprotion of issue mentions by party-wave in the CCD and salience in CCD journalist data
***************************

// Read in journalist data
use "2014_2015.dta", replace
append using "UK_Feb_March2015.dta"
append using "UK_Mar_Apr_2016.dta"

// Create wave variable
// subjects: 1 lab, 2 lib dem, 3 con, 4 ukip
sort article_date
tabulate article_date
tabulate subject
tabulate var_value
generate bes_wave=1 if article_date < td(20feb2014)
replace bes_wave=2 if article_date > td(20feb2014) & article_date < td(22may2014)
replace bes_wave=4 if article_date > td(22may2014) & article_date < td(04mar2015)
replace bes_wave=6 if article_date > td(04mar2015) & article_date < td(08may2015)
replace bes_wave=7 if article_date > td(08may2015) & article_date < td(14apr2016)
replace bes_wave=. if article_date > td(14apr2016)
summarize bes_wave

// Make EU and immigration into EU discussion
// Use both JournIssue (var_value) and JournIssueVal (valen_issue).
generate var_value2 = var_value
replace var_value2=13 if var_value2==8

generate valen_issue2 = valen_issue
replace valen_issue2=13 if valen_issue2==8

// Leave only issue discussions 
keep if statement_type=="JournIssue" | statement_type=="JournIssueVal"

// Create one issue variable
generate issue = var_value2
replace issue = valen_issue2 if valen_issue2!=.

// freqissue shows the counts of each var_value in each wave
// pctissue calculcates the proportion of each var_value in each wave
by bes_wave issue, sort: generate freqissue = _N 
by bes_wave: generate pctissue = 100 * freqissue/_N

// freqissueparty shows the counts of each issue for each party in each wave
// pctissueparty calculcates the proportion of each issue for each party in each wave
sort bes_wave subject issue
by bes_wave subject issue: generate freqissueparty = _N 
by bes_wave subject: generate pctissueparty = 100 * freqissueparty/_N

// Keep only relevant variables
keep bes_wave issue freqissue pctissue subject freqissueparty pctissueparty
// Keep only relevant issue area
keep if issue==13
// Keep only relevant parties
keep if subject>=1&subject<=4

// Create partyname variable
generate partyname = subject
tostring partyname, replace
replace partyname="Lab" if partyname=="1"
replace partyname="LD" if partyname=="2"
replace partyname="Con" if partyname=="3"
replace partyname="UKIP" if partyname=="4"

// Drop duplicates
drop subject
sort bes_wave partyname
duplicates drop
drop issue freqissue freqissueparty

// Save the data to use later
sort bes_wave partyname 
save "Data_ccdjourn", replace

// Merge party data with journalist data
use "Data_ccdparty.dta", replace
merge 1:1 bes_wave partyname using Data_ccdjourn.dta

// Add 0 to pctissueparty==.
replace pctissueparty=0 if pctissueparty==.

// Correlation for pctissueparty
pwcorr eutotal pctissueparty, sig star(.05)

***************************
** OA2: Correlation between CCD position and other position data
***************************

// expert data is available for waves 2 (BES), 4 (CHES), and 6 (BES)
// CHES values range from 1 to 7, with higher values being pro-EU.
// BES values range from zero to ten, with higher values being pro-EU.

// read in ches data - w4
frame create ches
frame ches: use 2014_CHES_dataset_means-2.dta
frame change ches

// Keep only UK data
tabulate cname
keep if cname=="uk"
tabulate party_name

// Keep only relevant parties
drop if party_name == "GREEN" | party_name == "PLAID" | party_name == "SNP"

// change party names to be consistent
replace party_name="Con" if party_name=="CONS"
replace party_name="Lab" if party_name=="LAB"
replace party_name="LD" if party_name=="LIBDEM"
rename party_name partyname

// create wave variable
generate bes_wave = 4

// Create normalized position variable
keep partyname bes_wave eu_position
gen eu_position_std = eu_position
sum eu_position_std
replace eu_position_std = (eu_position_std - `r(min)') / (`r(max)'-`r(min)')

// save the data to use later
sort partyname bes_wave
save Data_ches, replace

// merge ccd and ches
frame change default
drop _merge
merge 1:1 partyname bes_wave using Data_ches

// read in bes expert data - w2
frame create besexpert
frame besexpert: use BESExpert_Apr_May_2014.dta, clear
frame change besexpert

// create average positioning by experts
egen Lab = mean(EUintegrationLab01)
egen Con = mean(EUintegrationCon01)
egen LD = mean(EUintegrationLibDem01)
egen UKIP = mean(EUintegrationUKIP01)

// Keep only first row and relevant variables
keep if _n==1
keep Lab Con LD UKIP

// Transpose and rename columns
xpose, clear varname
rename v1 besmeanexpert
rename _varname partyname

// Assign bes wave
generate bes_wave = 2

// Create normalized position
gen besmeanexpert_std = besmeanexpert
sum besmeanexpert_std
replace besmeanexpert_std = (besmeanexpert_std - `r(min)') / (`r(max)'-`r(min)')
save Data_besexpert, replace

// read in bes expert data - w6
frame create besexpert6
frame besexpert6: use BESExpert_Apr_May_2015.dta, clear
frame change besexpert6

// create average positioning by experts
egen Lab = mean(EUIntegrationLab02)
egen Con = mean(EUIntegrationCon02)
egen LD = mean(EUIntegrationLibDem02)
egen UKIP = mean(EUIntegrationUKIP02)

// Keep only first row and relevant variables
keep if _n==1
keep Lab Con LD UKIP

// Transpose and rename columns
xpose, clear varname
rename v1 besmeanexpert
rename _varname partyname

// Assign bes wave
generate bes_wave = 6

// Add bes expert data
append using Data_besexpert
sort partyname bes_wave

// Create normalized position
sum besmeanexpert if bes_wave==6
replace besmeanexpert_std = (besmeanexpert - `r(min)') / (`r(max)'-`r(min)') if besmeanexpert_std==.

// Save data for later use
sort partyname bes_wave
save Data_besexpert, replace

// Merge ccd and bes expert data
frame change default
drop _merge
sort partyname bes_wave
merge 1:1 partyname bes_wave using Data_besexpert

// Create common expert position variable
generate expposition_std = eu_position_std
replace expposition_std = besmeanexpert_std if eu_position_std==.
drop eu_position besmeanexpert eu_position_std besmeanexpert_std

// Correlation between ccd position and expert position
pwcorr cmpLikeEUPosition expposition_std, sig star(.05)

*****************************
** OA5: Create control for expert position
*****************************

clear all 
set more off

// Read in the data
use Data_besparty.dta, clear

// Create respondent-party variable
tostring id, gen(id_str)
gen id_party = id_str + "_" + party
sort partyname bes

// Merge with ches data
merge m:1 partyname bes_wave using Data_ches
drop _merge 

// Merge with BES expert data
merge m:1 partyname bes_wave using Data_besexpert

// Create common expert position variable
generate expposition_std = eu_position_std
replace expposition_std = besmeanexpert_std if eu_position_std==.
drop eu_position besmeanexpert eu_position_std besmeanexpert_std

// reverse values
replace expposition_std = (expposition_std-1)*(-1)

*****************************
** OA5: Create (1) control for party's emphasis from CCD and (2) control for binary indicator for time salience
*****************************

// Merge bes data with ccdparty data
drop _merge
merge m:1 partyname bes_wave using Data_ccdparty, force

*****************************
** OA5: Create control for journalists' coverage bias from CCD
*****************************

// Read in journalist data
frame create journ
frame change journ
use "2014_2015.dta", replace
append using "UK_Feb_March2015.dta"
append using "UK_Mar_Apr_2016.dta"

// Create bes_wave variable
sort article_date
tabulate article_date
tabulate subject
generate bes_wave=1 if article_date < td(20feb2014)
replace bes_wave=2 if article_date > td(20feb2014) & article_date < td(22may2014)
replace bes_wave=4 if article_date > td(22may2014) & article_date < td(04mar2015)
replace bes_wave=6 if article_date > td(04mar2015) & article_date < td(08may2015)
replace bes_wave=7 if article_date > td(08may2015) & article_date < td(14apr2016)
replace bes_wave=. if article_date > td(14apr2016)
summarize bes_wave

// freqparty shows the counts of each party in each wave
// pctparty calculcates the proportion of each party in each wave
sort bes_wave subject
by bes_wave subject: generate freqparty = _N 
by bes_wave: generate pctparty = 100 * freqparty/_N

// Drop duplicates
keep bes_wave subject freqparty pctparty
duplicates drop
keep if subject>=1&subject<=4
drop if bes_wave==.

// Make partyname variable
generate partyname = subject
tostring partyname, replace
replace partyname="Lab" if partyname=="1"
replace partyname="LD" if partyname=="2"
replace partyname="Con" if partyname=="3"
replace partyname="UKIP" if partyname=="4"
drop subject freqparty
sort partyname bes_wave

// Save the journalist data for later use
save Data_journ, replace

// Merge bes with journ data
frame change default
drop _merge
merge m:1 partyname bes_wave using Data_journ

*****************************
** Table OA5.1-OA5.2
*****************************

reg perc_ch ChangeInImmigEuPosit expposition_std eutotal pctparty salience_binary copartisan age male education_numeric if party!="LD" & party!="UKIP", vce(cluster id)
est store updating_divided_controls
ologit partyPositionCertainty absChangeInImmigEuPosit expposition_std eutotal pctparty salience_binary copartisan age male education_numeric if party!="LD" & party!="UKIP", vce(cluster id)
est store certainty_divided_controls

reg perc_ch ChangeInImmigEuPosit expposition_std eutotal pctparty salience_binary copartisan age male education_numeric if party!="Con" & party!="Lab", vce(cluster id)
est store updating_united_controls
ologit partyPositionCertainty absChangeInImmigEuPosit expposition_std eutotal pctparty salience_binary copartisan age male education_numeric if party!="Con" & party!="Lab", vce(cluster id)
est store certainty_united_controls
